package com.pocket.demo.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * 两数之和
 *      https://leetcode.cn/problems/two-sum/?envType=study-plan-v2&envId=top-100-liked
 * @author zhaozhile
 */
public class _1_Array_TwoSum {

    public int[] twoSum(int[] nums, int target) {
        if(nums == null || nums.length == 0){
            return new int[0];
        }
        Map<Integer, Integer> recode = new HashMap<>();
        for(int i = 0; i < nums.length; i++){
            int other = target - i;
            if(recode.containsKey(other)){
                return new int[]{i, recode.get(other)};
            } else {
                recode.put(nums[i], i);
            }
        }
        return new int[0];
    }

    public static void main(String[] args) {

    }

}
